package com.pengshujian.aidlclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.pengshujian.aidlserver.Book;
import com.pengshujian.aidlserver.BookController;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "Client";
    private Button btnGetBookList;
    private Button btnAddBookInOut;
    private Button btnSendCommand;
    private BookController bookController;
    private boolean connected;
    private List<Book> bookList;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            bookController = BookController.Stub.asInterface(iBinder);
            connected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            connected = false;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnGetBookList = findViewById(R.id.btnGetBookList);
        btnAddBookInOut = findViewById(R.id.btnAddBookInOut);
        btnSendCommand = findViewById(R.id.btnSendCommand);

        btnGetBookList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(connected){
                    try {
                        bookList = bookController.getBookList();
                    }catch (RemoteException e){
                        e.printStackTrace();
                    }

                    log();
                }
            }
        });

        btnAddBookInOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(connected){
                    Book book = new Book("这是一本新书InOut");
                    try{
                        bookController.addBookInOut(book);
                    }catch (RemoteException e){
                        e.printStackTrace();
                    }
                }
            }
        });

        btnSendCommand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(connected){
                    try{
                        bookController.sendCommand("hello this is my command");
                    }catch (RemoteException e){
                        e.printStackTrace();
                    }
                }
            }
        });

        bindService();
    }

    private void bindService(){
        Intent intent = new Intent();
        intent.setPackage("com.pengshujian.aidlserver");
        intent.setAction("com.pengshujian.aidlserver.action");
        bindService(intent,serviceConnection, Context.BIND_AUTO_CREATE);
    }

    private void log(){
        for(Book book:bookList){
            Log.e(TAG,book.toString());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(connected){
            unbindService(serviceConnection);
        }
    }
}